/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ai;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;

/**
 *
 * @author root
 */
public class KnowledgeBase {
    
    public KnowledgeBase(LinkedList<MapExp> pMapExpList, LinkedList<ImplyExp> pImplyExpList){
        mMapExpList = pMapExpList;
        mImplyExpList = pImplyExpList;
        
        mLabels = new HashSet<String>();
        for(MapExp m : mMapExpList){
            for(int i = 0; i < m.getLabelCount(); i++){
                mLabels.add(m.getLabel(i).getName());
            }
        }
        
        /*for(ImplyExp ie : mImplyExpList){
            System.out.println(ie.toString());            
        }*/
    }

    public HashMap<String, String> findTranslationTable(String pSourceLabel, String pTargetLabel){
        for(MapExp m : mMapExpList){
            HashMap<String, String> lTemp = m.getMap(pSourceLabel, pTargetLabel);
            if (lTemp != null){
                return lTemp;
            }
        }        
        return null;
    }
    
    public boolean isWordCategory(String pLabel){
        return mLabels.contains(pLabel);
    }
    
    public boolean isWordInCategory(String pWord, String pLabel){
        for(MapExp me : mMapExpList){
            int lIndex = me.findLabel(pLabel);
            if(lIndex != -1){
                if( me.hasWord(pWord, lIndex) ){
                    return true;
                }
            }
        }
        return false;
    }
    
    public LinkedList<SerialExp> findRewritesFor(String pLabel) throws ParseException{
        LinkedList<SerialExp> lList = new LinkedList<SerialExp>();
        for(ImplyExp ie : mImplyExpList){
            String lLeftLabel = ie.getLeft().getName().trim();
            // see if we've got the right label.
            //System.out.println("findRewritesFor, Gram lablel: " + lLeftLabel + " looking for label: " + pLabel+ " lLeftLabel.equals(pLabel) " + lLeftLabel.equals(pLabel));
            if(lLeftLabel.equals(pLabel)){
                GramExp lRight = ie.getRight();
                if( lRight.getClass() == SerialExp.class){
                    lList.add((SerialExp)lRight);
                    //System.out.println("lRight is a SerialExp: " + (SerialExp)lRight);
                }
                else if( lRight.getClass() == ThatOtherOrExp.class ){
                    for(SerialExp se : ((ThatOtherOrExp)lRight).getExpList()){
                        lList.add(se);
                    }
                    //System.out.println("lRight is a ThatOtherOrExp: " + lList);
                }
                else{
                    throw new ParseException("KnowledgeBase.findRewritesFor(" + pLabel + ") Error in imply expression " + ie.toString());
                }
            }
        }
        return lList;
    }
    
    private  LinkedList<MapExp> mMapExpList;
    private  LinkedList<ImplyExp> mImplyExpList;
    private  HashSet<String> mLabels;
}
